/*
	module name	:	CInlineHook
	author		:	Jackozoo
	date		:	2011-5
	usage		:	AddHook & AddNakedHook
*/

#ifndef _INLINE_HOOK_
#define _INLINE_HOOK_

#include <stdio.h>
#include <windows.h>

#define S_EFL(x) (*(x-1))
#define S_EAX(x) (*(x-2))
#define S_ECX(x) (*(x-3))
#define S_EDX(x) (*(x-4))
#define S_EBX(x) (*(x-5))
#define S_ESP(x) (*(x-6)+4)
#define S_EBP(x) (*(x-7))
#define S_ESI(x) (*(x-8))
#define S_EDI(x) (*(x-9))


void ConvertHexToASCii(void* pBuf, int nBuffLen,char* header);

#define CALL_OFFSET		(3+4+2)
#define JMP_OFFSET		(CALL_OFFSET+4+3+2+2)


//#define SAFE_DELETE(a) if (a) {delete (a); (a) = 0;}
//#define SAFE_DELETE_ARRAY(a) if (a) {delete[] (a); (a) = 0;}

#define SHELLCODE_LEN	(0x1000)

#define JACKYEND	\
	__asm __emit	0x6A\
	__asm __emit	0x61\
	__asm __emit	0x63\
	__asm __emit	0x6B\
	__asm __emit	0x79\
	__asm __emit	0x65\
	__asm __emit	0x6E\
	__asm __emit	0x64\

typedef	void (__cdecl *PFNANALFUNC)(DWORD *);
typedef	void (__cdecl *PFNANALFUNCWITHPARAM)(DWORD *, PVOID);
typedef void (__cdecl *PFNNAKEDFUNC)();

#pragma pack(push,1)
typedef	struct tagHOOKSTRUCT 
{
	DWORD	dwHookId;
	DWORD	dwHookAddr;

	/*
	0x9C,								pushfd
	0x60,								pushad
	0x54,								push	esp
	0x83,0x04,0x24,0x24,				add		dword ptr [esp], 24h
	0xFF,0x15,0x00,0x00,0x00,0x00,		call	dword ptr [0]
	0x83,0xC4,0x04,						add		esp, 4
	0x61,								popad
	0x9D,								popfd
	0xFF,0x25,0x00,0x00,0x00,0x00		jmp		dword ptr [0]
	*/
	BYTE			NakedStub[29];
	BYTE			InstLen;
	BYTE			OriCode[16];
	DWORD			dwJmpAddr;
	DWORD			dwfnHook;
	BYTE			flag;
	tagHOOKSTRUCT	*next;
	BYTE			*nakedcode;
}HOOKSTRUCT,*PHOOKSTRUCT;
#pragma pack(pop)

class CInlineHook  
{
public:
	CInlineHook();
	virtual ~CInlineHook();
	
	PHOOKSTRUCT	HookList;
	DWORD		m_dwFirstId;

public:
	DWORD	AddHook(DWORD dwAddress,PFNANALFUNC fnHook);
	DWORD	AddHookWithParam(DWORD dwAddress,PFNANALFUNCWITHPARAM fnHook, PVOID pParam);
	DWORD	AddNakedHook(DWORD dwAddress,PFNNAKEDFUNC fnNakedHook);
	VOID	Unhook(DWORD HookID);
	VOID	UnhookAll();
};

#endif
